import {defineStore} from "pinia";
import axios from "axios";
import { useRouter } from "vue-router";
import {baseURL} from "@/request/index.js";
const router=useRouter();

export const useSongsStore=defineStore('songs',{
    state:()=>{
        return{
            //搜索信息
            searchvalue:'',   //搜索关键字
            List:[],
            searchAlName:'',
            searchAlPic:'',
            searchAlId:'',
            singername:'', //歌手名
            playcount:0, //歌曲数
            alcount:0,   //专辑数
            avatarUrl:'',  //头像
            singerid:'',  //歌手id
            singertrans:'', //歌手翻译
            MVmessage:[],//歌手视频信息
            album:[],//歌手专辑信息
            firstAlUrl:"",
            firstAlName:"",
            playList:[],//歌手歌单信息
            desc:[],//获取歌手描述
            allsimisinger:[], //所有相关歌手
            singersList:[],
            songs:[],

            //当前播放信息
            songid:'',
            picUrl:'',
            songname:'',
            singer:'',
            al:'',
            lrc:'',
            songUrl:'',
            currentTime:0,
            totalTime:300,

            ifplay:false,  //控制音乐播放和暂停
            ifpause:false,
            show:1,  //控制组件显示
            listType:0, //播放列表类型
        }
    },

    getters:{

        },
    actions:{
        async search(value) {
            this.searchvalue=value;
            const List1 = await axios.post(baseURL + "/search?keywords=" + value); //搜索
            this.List = List1.data.result.songs;
            console.log(this.List)

            this.searchAlName = '';   //搜索是否为专辑名
            this.searchAlPic = '';
            if (this.List[0].album.name.toUpperCase() === value.toUpperCase()) {
                this.searchAlName = value;
                this.searchAlId = this.List[0].album.id;
                axios.post(baseURL + "/album?id=" + this.List[0].album.id).then((res) => {
                    console.log(res);
                    this.searchAlPic = res.data.album.picUrl;
                })
            }

            let search = false;
            if (this.List[0].name.toUpperCase() === value.toUpperCase()) {  //搜索是否为歌名
                search = true;
            }
            for await (let item of this.List[0].artists) {
                if (item.name.toUpperCase() === value.toUpperCase()) {    //搜索是否为歌手名
                    search = true;
                    this.singerid = item.id;
                }
            }
            if (search === false) {
                this.singerid = this.List[0].artists.id;
            }

            if (this.singerid) {
                const a = axios.post(baseURL + "/artists?id=" + this.singerid);  //获取歌手详情
                const b = axios.post(baseURL + "/artist/desc?id=" + this.singerid) //获取歌手描述
                const c = axios.post(baseURL + "/artist/mv?id=" + this.singerid + "&limit=50") //获取歌手视频
                const d = axios.post(baseURL + "/artist/album?id=" + this.singerid + "&limit=50") //获取歌手专辑

                const [List2, List3, List4, List5] = await Promise.all([a, b, c, d])
                console.log(List5)
                this.avatarUrl = List2.data.artist.picUrl;
                this.singername = List2.data.artist.name;
                this.singertrans = List2.data.artist.trans;
                this.playcount = List2.data.artist.musicSize;
                this.alcount = List2.data.artist.albumSize;
                this.songs = List2.data.hotSongs;
                this.desc = List3.data;
                this.MVmessage = List4.data.mvs;
                this.album = List5.data.hotAlbums;
                this.firstAlUrl = List5.data.hotAlbums[0].picUrl;
                this.firstAlName = List5.data.hotAlbums[0].name;

            }else{
                this.avatarUrl='';
                this.firstAlUrl='';
                this.MVmessage=[];
                this.album=[];
                this.desc=[];
                this.songs=[];

            }
        },
            getsearchMV(){
                this.List.forEach((item)=>{
                axios.post(baseURL+"/mv/url?id=" + item.mvid).then((res) => { //获取MV地址
                this.mvUrl.push(res.data.data.url);
            })
        })
    },
           
         getcurrentplay(value){
                axios.post(baseURL+"/song/detail?ids="+value).then((res)=>{ //获取歌曲详细信息
                         // console.log(res)
                        this.songid=value;
                        this.picUrl=res.data.songs[0].al.picUrl;
                        this.songname= res.data.songs[0].name;
                        this.singer=res.data.songs[0].ar[0].name;
                        this.al=res.data.songs[0].al.name;
                })
            },
            getlyc(value){
                axios.post(baseURL+"/lyric?id="+value).then((res)=>{
                    // console.log(res.data.lrc.lyric);
                    this.lrc=res.data.lrc.lyric;
                })
            },
            getSongUrl(value){
               this.songUrl=value;
            },
            readyplay(){
                this.ifpause=false;
                this.ifplay=false;
                this.ifplay===false? this.ifplay=true:this.ifplay=false;
            },
            getcurrentTime(value){
                this.currentTime=value;
                // console.log(this.currentTime)
            },
            gettotalTime(value){
                this.totalTime=value;
            }
        },

})


export const useplayListStore=defineStore('playList',{
    state:()=>{
        return{
            //歌单信息
            id:'',
            playListUrl:'',
            playListdescription:'',
            playListName:'',
            List:[],
            tags:[],
            nickname:'',
            avatarUrl:'',
            subscribedCount:'',
            playCount:'',
        }
    },
        actions:{
            getcurrentplayList(id){
                this.List=[];
                axios.post(baseURL+"/playlist/detail?id="+id).then((res)=>{//获取推荐歌单详情
                    console.log(res);
                    this.playListName=res.data.playlist.name;
                    this.playListdescription=res.data.playlist.description;
                    this.playListUrl=res.data.playlist.coverImgUrl;
                    this.tags=res.data.playlist.tags;
                    this.id=res.data.playlist.id;
                    this.avatarUrl=res.data.playlist.creator.avatarUrl;
                    this.nickname=res.data.playlist.creator.nickname
                    this.subscribedCount=res.data.playlist.subscribedCount;
                    this.playCount=res.data.playlist.playCount;
                    res.data.privileges.forEach((item)=>{
                        axios.post(baseURL+"/song/detail?ids="+item.id).then((res)=>{
                            // console.log(res);
                            this.List.push(res.data.songs)
                        })
                    })
                    console.log(this.List)
                })
            }
        }

})

export const usealbumDetailStore=defineStore('albumdetail',{
    state:()=>{
        return{
                albumID:'',
                albumPicUrl:'',
                albumName:'',
                albumSinger:'',
                publishTime:'',
                songsList:[],
        }
    },
    actions: {
        getalbumDetail(album_id) {
            this.albumID=album_id;
            this.songsList=[];
            axios.post(baseURL+"/album?id=" + album_id).then((res) => {
                console.log(res);
                this.albumPicUrl = res.data.album.picUrl;
                this.albumName = res.data.album.name;
                this.albumSinger = res.data.album.artist.name
                this.publishTime = res.data.album.publishTime;
                res.data.songs.forEach((item) => {
                    axios.post(baseURL+"/song/detail?ids=" + item.id).then((res) => {
                        this.songsList.push(res.data.songs)
                    })
                })
                console.log(this.songsList);
            })
        }
    }
})

export const useUserStore=defineStore('user', {
    state: () => {
        return {
            user:{}
        }

    },
    actions:{
        setCath(key,value) {
            window.localStorage.setItem(key, JSON.stringify(value))
        },
        // 查找
        getCache(key) {
            // obj=>sting=>obj
            const value = window.localStorage.getItem(key)
            if (value) {
                return JSON.parse(value)
            }
        },
        // 删除
        deleteCatch(key) {
            window.localStorage.removeItem(key)
        },
        // 清理
        clearCache() {
            window.localStorage.clear()
        },
        login(){
        axios.post(baseURL+"/user/detail?uid="+5061695424).then(res=>{
            this.user=res.data;
            console.log(this.user)
            this.setCath("user",this.user)
        })
     }
    }
})

export const useMVStore=defineStore("mv",{
    state: () => {
        return {
            mvid:0,
            mvurl:"",
            mvmessage:{},
        }

    },
    actions: {
        async playmv(mvid) {
            this.mvmesage =await axios.post(baseURL+"/mv/url?id=" + mvid) //获取mv数据
            console.log(this.mvmesage)
            this.mvurl=this.mvmesage.data.data.url;
        }
    }
})

